package servlet;

import Dao.DBUtil;
import Dao.UserDao;
import eneity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        resp.setContentType("text/html; charset=utf8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        int ret = 0;
        try {
            ret = UserDao.insertUser(user);
        } catch (SQLException e) {
            System.out.println("insert 失败");
        }
        if (ret == 1){
            System.out.println("注册成功!");
            resp.sendRedirect("login.jsp");
        }else {
            System.out.println("注册失败!");
        }
    }
}
